<?php

namespace app\common\model;
use think\Model;
use think\Db;

class EnameModel extends Model
{
    protected $name = 'ename';



    public function getEnamesByWhere($map, $Nowpage, $limits)
    {

      
        return $this->alias('e')->field('e.*,m.account,m.nickname')->join("member m", "e.daili_id = m.id",'LEFT')->where($map)->page($Nowpage, $limits)->order('e.type asc,e.id asc')->select();
    }

    public function getEnamesByType($map)
    {
        $data = $this->where($map)->order('type')->select();
        foreach($data as $key => &$vo)
		{
			$suns = Db::name('member')->where(['ename_first' => $vo['ename'],'status' => 1])->count();
			$data[$key]['ename'] = $vo['ename'];
			$data[$key]['ip']    = $vo['ip'];
		    $data[$key]['suns']  = $suns;
		}
		
        return $data;
    }

    public function getOneEname($id)
    {
        return $this->where('id', $id)->find();
    }

    public function insertEname($param)
    {
        try
        {
    		$for = explode("\n", $param['ename']);
			foreach($for as $vo)
			{
				$add[] = array('ename' => $vo, 'status' => $param['status'],'type' => $param['type']);
			}
			$result = $this->insertAll($add);
            if (false === $result)
            {
                return ['code' => -1, 'data' => '', 'msg' => $this->getError()];
            }
            else
            {
            	writelog(session('uid'), session('username'), '域名【' . $param['ename'] . '】添加成功', 1);
            	return ['code' => 1, 'data' => '', 'msg' => '添加' . count($for) . '条域名成功'];
            }
        }
        catch(PDOException $e)
        {
            return ['code' => -2, 'data' => '', 'msg' => $e->getMessage()];
        }
    }

    /**
     * 编辑管理员信息
     * 
     * @param  $param 
     */
    public function editEname($param)
    {
        try
        {
            
            if(!empty($param['luodi'])){
                 $for = explode("\n", $param['luodi']);
                 $for = json_encode($for);
                 $param['luodi'] = $for;
            }
            
 
            $result = $this->validate('EnameValidate')->allowField(true)->save($param, ['id' => $param['id']]);
            if (false === $result)
            {
                return ['code' => 0, 'data' => '', 'msg' => $this->getError()];
            }
            else
            {
                writelog(session('uid'), session('username'), '域名【' . $param['ename'] . '】编辑成功', 1);
                return ['code' => 1, 'data' => '', 'msg' => '编辑域名成功'];
            }
        }
        catch(PDOException $e)
        {
            return ['code' => 0, 'data' => '', 'msg' => $e->getMessage()];
        }
    }

    public function delEname($id)
    {
        try
        {
        	$this->where('id', $id)->delete();
            Db::name('auth_group_access')->where('uid', $id)->delete();
            writelog(session('uid'), session('username'), '管理员【' . session('username') . '】删除域名成功(ID=' . $id . ')', 1);
            return ['code' => 1, 'data' => '', 'msg' => '删除域名成功'];
        }
        catch(PDOException $e)
        {
            return ['code' => 0, 'data' => '', 'msg' => $e->getMessage()];
        }
    }

    public function delEname_wxdanger()
    {
        try
        {
    		$wxdanger = $this->where(['zt' => 0,'type' => 3])->select();
    		if ($wxdanger)
	        {
	        	$count = 0;
	            foreach ($wxdanger as $value)
	            {
	                $id = $value['id'];
	                self::delEname($id);
	                $count++;
	            }
	            return ['code' => 1, 'data' => '', 'msg' => '删除'. $count .'条域名成功'];
	        }
	        else
	        {
	        	return ['code' => 1, 'data' => '', 'msg' => '没有符合条件的域名'];
	        }
        }
        catch(PDOException $e)
        {
            return ['code' => 0, 'data' => '', 'msg' => $e->getMessage()];
        }
    }
}
